<?php
namespace app\common\logic;

use app\common\model\Cutdown;
use app\common\model\Goods;
use think\Db;

/**
 * 砍价逻辑定义
 * Class CatsLogic
 * @package common\Logic
 */
class CutdownLogic extends Prom
{
    protected $Cutdown;//砍价活动模型
    protected $goods;//商品模型

    public function __construct($goods)
    {
        parent::__construct();
        $this->goods = $goods;

        //活动商品没有规格，活动和规格是一对多
        $this->Cutdown = Cutdown::get($goods['prom_id'],'',true);

        if ($this->Cutdown) {
            //每次初始化都检测活动是否失效，如果失效就更新活动和商品恢复成普通商品
            if ($this->checkActivityIsEnd() && $this->Cutdown['is_end'] == 0) {

                Db::name('goods')->where("id", $this->goods['id'])->update(['prom_type' => 0, 'prom_id' => 0]);

                $this->Cutdown->is_end = 1;
                $this->Cutdown->save();
                unset($this->goods);
                $this->goods = Goods::get($goods['id'],'',true);
            }
        }
    }

    public function getPromModel(){
        return $this->Cutdown;
    }

    /**
     * 活动是否正在进行
     * @return bool
     */
    public function checkActivityIsAble(){
        if(empty($this->Cutdown)){
            return false;
        }
        if(time() > $this->Cutdown['start_time'] && time() < $this->Cutdown['end_time'] && $this->Cutdown['status'] == 1 && $this->Cutdown['is_end'] == 0){
            return true;
        }
        return false;
    }

    /**
     * 活动是否结束
     * @return bool
     */
    public function checkActivityIsEnd(){
        if(empty($this->Cutdown)){
            return true;
        }
        if(time() > $this->Cutdown['end_time']){
            return true;
        }
        return false;
    }

    /**
     * 获取商品原始数据
     * @return Goods
     */
    public function getGoodsInfo()
    {
        return $this->goods;
    }

    /**
     * 该活动是否已经失效
     */
    public function IsAble(){
        if(empty($this->Cutdown)){
            return false;
        }
        if(time() > $this->Cutdown['end_time']){
            return false;
        }
        if($this->Cutdown['status'] != 1){
            return false;
        }
        if($this->Cutdown['is_end'] == 1){
            return false;
        }
        return true;
    }

    /**
     * 获取商品转换活动商品的数据
     * @return static
     */
    public function getActivityGoodsInfo(){
        $activityGoods = $this->goods;
        $activityGoods['activity_title'] = $this->Cutdown['description'];
        $activityGoods['market_price'] = $this->goods['market_price'];
        $activityGoods['start_time'] = $this->Cutdown['start_time'];
        $activityGoods['end_time'] =  $this->Cutdown['end_time'];
        $activityGoods['shop_price'] = $this->Cutdown['price'];
        $activityGoods['zuigao_text'] = ($this->Cutdown['price']*100 - $this->Cutdown['min_price']*100)/100;
        $activityGoods['item_id'] = $this->Cutdown['item_id'];

        return $activityGoods;
    }




    /**
     * 一天只能砍价一次
     *
     */
    public function isFirstCut($cutdown_follow_id,$user_id){
        $isCut = Db::name('cutdown_list')->where([
                'cutdown_follow_id' => $cutdown_follow_id,
                'user_id' => $user_id,
            ])->whereTime('createtime','today')->count();




        if($isCut){
            $data = $this->getKanJiaYeMian($cutdown_follow_id);
            return ['code' => 0, 'msg' => '每天只能帮好友砍价一次，明天再来哦','data' => $data];

        }else{
            return ['code' => 1, 'msg' => '可砍', 'data' => ''];
        }
    }

    /**
     * 是否在砍价有效期内
     */
    public function isInTime($cutdown_follow_id){
        $cutdownFollowData = Db::name('cutdown_follow')->find($cutdown_follow_id);
        $time_limit = Db::name('cutdown')->where(['id' => $cutdownFollowData['cutdown_id']])->value('time_limit');

        if( intval($cutdownFollowData['createtime'])+ intval($time_limit) >= time() ){
            return true;

        }else{
            return false;
        }
    }

    public function getKanJiaYeMian($cutdown_follow_id){
        $cutdownFollowM = new \app\common\model\CutdownFollow();
        $cutdownFollow = $cutdownFollowM->get($cutdown_follow_id);
        $cutdownFollow->cutdown;
        $cutdownFollow->goods;
        $cutdownFollow->cutdown_list;


        $arr = $cutdownFollow->toArray();

        if($arr['cutdown_list']){
            foreach ($arr['cutdown_list'] as $k => $v){
                $user = Db::name('user')->find($v['user_id']);
                $arr['cutdown_list'][$k]['nickname'] = $user['nickname'];
                $arr['cutdown_list'][$k]['avatar'] = $user['avatar'];

            }

        }
        $arr['yikan'] = ($arr['cutdown']['price'] * 100 - $arr['deal_price'] * 100) / 100;
        $arr['haikekan'] = ($arr['deal_price'] * 100 - $arr['cutdown']['min_price'] * 100) / 100;

        $return = [
            'cutdown_follow_id' => $arr['id'],
            'cutdown_id' => $arr['cutdown']['id'],
            'description' => $arr['cutdown']['description'],
            'sales_sum' => $arr['cutdown']['sales_sum'],
            'price' => $arr['cutdown']['price'],
            'image' => $arr['goods']['image'],
            'yikan' => $arr['yikan'],
            'haikekan' => $arr['haikekan'],
            'time_limit' => $arr['cutdown']['time_limit'],
            'cut_count' => $arr['cut_count'],
            'cutdownList' => $arr['cutdown_list']

        ];

        return $return;


    }
}